
#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    long long sum(int n)
    {
        long long ret = 0;
        while (n)
        {
            int bit = n % 10;
            ret += bit * bit;
            n /= 10;
        }
        return ret;
    }

    bool isHappy(int n) {
        long long sum_slow = n, sum_fast = n;

        do
        {
            sum_slow = sum(sum_slow);
            sum_fast = sum(sum_fast);
            sum_fast = sum(sum_fast);
        } while (sum_slow != sum_fast);

        return sum_slow == 1;
    }
};